package com.gxa.weixing.portal.controller;

import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.util.StringUtil;
import com.gxa.weixing.portal.service.SmsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 发送短信的服务接口
 *
 * @author YXQ
 * @date 2021/11/29 15:23
 */
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsService smsService;

    @GetMapping("/send")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "phone",required = true,dataType = "string",paramType = "query")
    })
    public Map<String, Object> sendSms(String phone) throws BusinessException {
        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException(ExceptionCode.NO_PARAM);
        }
        if (!StringUtil.isPhoneNumber(phone)) {
            throw new BusinessException(ExceptionCode.WRONG_PHONE);
        }
        String s = smsService.sendLoginSms(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("短信发送成功");
        jsonResult.setData(s);
        return jsonResult.getDataMap();
    }
}
